package com.example.phpclient;

import android.graphics.Color;
import android.os.Bundle;

import com.example.phpclient.viewpager_tablayout.adapter.IndicatorAdapter;
import com.example.phpclient.viewpager_tablayout.adapter.MainContentAdapter;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

public class manager_activtiy extends FragmentActivity {
    private MagicIndicator magicIndicator;
    private CommonNavigator commonNavigator;
    private ViewPager contentViewpager;
    private IndicatorAdapter mIndicatorAdapter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewpager_tablayout);
        initView();
        initEvent();
    }

    private void initEvent() {
        mIndicatorAdapter.setOnIndicatorTapClickListener(new IndicatorAdapter.OnIndicatorTapClickListener() {
            @Override
            public void onTabClick(int index) {
                //点击时改变pager
//                Log.d(TAG, "onTabClick: ");
                if (contentViewpager!=null)
                {
                    contentViewpager.setCurrentItem(index);
                }

            }
        });
    }

    private void initView() {
        magicIndicator =this.findViewById(R.id.main_indicator);
        magicIndicator.setBackgroundColor(Color.WHITE);
        //创建适配器
        mIndicatorAdapter=new IndicatorAdapter(this);
        commonNavigator =new CommonNavigator(this);
        commonNavigator.setAdjustMode(true);//自我调节评分位置
        commonNavigator.setAdapter(mIndicatorAdapter);
        //Viewpager
        contentViewpager=this.findViewById(R.id.content_pager);
        //滑动的监听事件
        contentViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //创建内容适配器
        FragmentManager supportFragmentManager=getSupportFragmentManager();
        MainContentAdapter mainContentAdapter = new MainContentAdapter(supportFragmentManager);
        contentViewpager.setAdapter(mainContentAdapter);
        //绑定

        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator,contentViewpager);
    }
}
